package com.library.controller;

import com.library.service.AdminService;
import com.github.pagehelper.PageInfo;
import com.library.po.Admin;
import com.library.utils.DataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;

import static com.library.utils.Constants.FAIL_MSG;

@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;

    @GetMapping("/adminIndex")
    public String adminIndex(){
        return "admin/adminIndex";
    }

    @RequestMapping("/adminAll")
    @ResponseBody
    public DataInfo queryAdminAll(Admin admin, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "15") Integer limit){
        PageInfo<Admin> pageInfo = adminService.queryAdminAll(admin,page,limit);
        return DataInfo.ok("成功",pageInfo.getTotal(),pageInfo.getList());
    }

    @GetMapping("/adminAdd")
    public String adminAdd(){
        return "admin/adminAdd";
    }

    @GetMapping("/queryAdminById")
    public String queryAdminById(Integer id, Model model){
        Admin admin = adminService.queryAdminById(id);
        model.addAttribute("admin",admin);
        return "admin/updateAdmin";
    }
}
